package com.velpro.x1.user.config;


import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import java.net.InetAddress;

/**
 * 自定义数据库主键生成策略
 * https://baomidou.com/pages/568eb2/#spring-boot
 */
public class CustomIdGenerator implements IdentifierGenerator {

    private final Sequence sequence;

    public CustomIdGenerator() {
        this.sequence = new Sequence((InetAddress)null);
    }

    public CustomIdGenerator(InetAddress inetAddress) {
        this.sequence = new Sequence(inetAddress);
    }

    public CustomIdGenerator(long workerId, long dataCenterId) {
        this.sequence = new Sequence(workerId, dataCenterId);
    }

    public CustomIdGenerator(Sequence sequence) {
        this.sequence = sequence;
    }

    /*
     *自定义ASSIGN_ID方法的ID生成
     *可以根据自己的业务进行重新实现，这里copy默认的DefaultIdentifierGenerator实现
     */
    @Override
    public Long nextId(Object entity) {
        return this.sequence.nextId();
    }

    /**
     * 自定义ASSIGN_UUID方法的ID生成
     * 这里为接口的默认实现，可根据业务需求重新定义
     * @param entity
     * @return
     */
    @Override
    public String nextUUID(Object entity){
        return IdWorker.get32UUID();
    }

}
